package com.ruoyi.web.controller.vantController.orderController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderComment;
import com.ruoyi.system.domain.zhuangxiumain.vo.OrderCommentVo;
import com.ruoyi.system.service.vantservice.customerervice.ApiLocalFabulousService;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderCommentService;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderDetailService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import java.util.Date;
import java.util.Map;

/**
 * Created by 惠普 on 2020/12/28 17:35
 */
@RestController
@RequestMapping("/api/order/comment")
@CrossOrigin
public class ApiOrderCommentController {
    @Resource
    private ApiOrderCommentService apiOrderCommentService;
    @Resource
    private ApiOrderDetailService apiOrderDetailService;
    @Resource
    private ApiLocalFabulousService apiLocalFabulousService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private HttpServletRequest request;

    /**
     * 添加评论信息
     * @param orderComment
     * @return
     */
    @PostMapping("/orderCommentAdd")
    public AjaxResult orderCommentAdd(@RequestBody OrderComment orderComment){
        try {
            // 获取前台登录用户信息
            CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
            orderComment.setCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
            orderComment.setOrderCommentApplicationTime(new Date());
            int row = apiOrderCommentService.addOrderComment(orderComment);
            if(row==1){
                return AjaxResult.success(apiOrderDetailService.updateOrderDetail(orderComment.getOrderId()));
            }else {
                return AjaxResult.error("评论失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("发生错误");
        }
    }

    /**
     * 用户全部的评价
     * @param orderCommentVo
     * @return
     */
    @PostMapping("/orderCommentVoByNull")
    public AjaxResult orderCommentVoByNull(@RequestBody OrderCommentVo orderCommentVo){
        try {
            // 获取前台登录用户信息
            CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
            orderCommentVo.setCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
            return AjaxResult.success(apiOrderCommentService.selectOrderCommentVo(orderCommentVo));
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("发生错误");
        }
    }

    /**
     * 上传多图片
     * @param files
     * @return
     */
    @PostMapping("/upload")
    public Map<String,Object> up(@RequestParam("files") MultipartFile[] files){
        return apiLocalFabulousService.uploadImages(files);
    }


}
